package com.liangdemo.web.thread;

/**
 *
 */
public class RunnableDemo3 implements Runnable {
    public String name = null;
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {
                try {
                    Thread.sleep(500);

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }


            if (fa(i)) {

                break;
            }

            try {
                System.out.println(Thread.currentThread().getName() + "跑了" + i);
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public boolean fa(int i) {
        if (name != null) {
            return true;
        }
        if (i >= 100) {
            name = Thread.currentThread().getName();

            System.out.println("获胜者" + name);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        RunnableDemo3 r1 = new RunnableDemo3();
        new Thread(() -> {
            r1.run();
        }, "兔子").start();
        new Thread(() -> {
            r1.run();
        }, "乌龟").start();
    }
}
